﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace LicPdaService
{
    public class PDAXmlMLInLotProcess
    {
        /// <summary>
        /// 7.0 通过ML获取库存 Add By Quinn.Ling 201703
        /// DataSource:Inventory
        /// </summary>
        
        public static string GetPDAMLInLotData(XmlDocument doc)
        {
            #region 通用变量定义
            string docResult = string.Empty; //返回值
            string userid = string.Empty;
            string whseid = string.Empty;
            string master_label = string.Empty;
            string sku = string.Empty;
            string sku_sm = string.Empty;
            string customer_code = string.Empty;
            string loc = string.Empty;
            string lot = string.Empty;
            string lpn = string.Empty;
            string qty = string.Empty;
            string serial = string.Empty;
            string return_status = string.Empty;
            string lottable01 = string.Empty; //批次
            string slpn = string.Empty; //条件托盘号
            List<string> sqllists = new List<string>();
            string strSql = string.Empty;

            XmlDocument docreturn = new XmlDocument();//返回报文
            XmlDeclaration dec = docreturn.CreateXmlDeclaration("1.0", "utf-8", null);//建立Xml的定义声明  
            docreturn.AppendChild(dec);
            XmlElement rootResult = docreturn.CreateElement("NewDataSet"); //创建根节点
            #endregion

            string[] s = null;
            try
            {
                #region  参数源
                XmlNode root = doc.DocumentElement; // 报文根节点
                XmlNode nodeSku = root.SelectSingleNode("sku");
                XmlNodeList xmlnodelist = nodeSku.ChildNodes;

                userid = nodeSku.SelectSingleNode("userid").InnerText;
                whseid = nodeSku.SelectSingleNode("whseid").InnerText;
                master_label = nodeSku.SelectSingleNode("master_label").InnerText;
                try
                {
                    s= master_label.Split(new char[] { '|' }); //扫描为Mast_label
                    if (s.Length>1)
                    {
                        sku = s[0].ToString();
                        lottable01 = s[2].ToString();
                    }
                    else
                    {
                        if (App.IsNumber(master_label)) //判定是否为纯数字-纯数字为Sereail号码，否则为托盘号Lpn
                        {
                            serial = master_label; //扫描为Sereail号
                        }
                        else
                        {
                            if (master_label.Length == 6)
                            {
                                loc = master_label; //扫描为库位
                            }
                            else
                            {
                                if (master_label.StartsWith("L"))
                                {
                                    slpn = master_label; //扫描为托盘号
                                }
                                else
                                {
                                    sku_sm = master_label; //扫描为料号
                                }
                            }
                        }

                    }
                  
                }
                catch( Exception ex) {
                    App.Log("PDAXmlMLInLotProcess.cs-2", ex.Message.ToString());
                }
                if (s.Length>1) //扫描为Mast_label
                {
                    strSql = "SELECT I.customer_code,I.sku,L.lottable01,I.loc,I.lot,I.lpn,(I.qty-I.allocated_qty-I.picked_qty) as qty  ";
                    strSql += "FROM Inventory AS I left join LotAttribute AS L  on I.lot=L.lot  and I.whseid=L.whseid WHERE  I.sku='" + sku + "' AND I.whseid='"+whseid+ "' and (I.qty-I.allocated_qty-I.picked_qty)>0";
                    //strSql += " AND L.lottable01='" + lottable01 + "'";
                }
                else
                {
                    if (App.IsNumber(master_label)) //扫描为Sereail号
                    {
                        strSql = "SELECT I.customer_code,I.sku,L.lottable01,I.loc,I.lot,I.lpn,(I.qty-I.allocated_qty-I.picked_qty) as qty  ";
                        strSql += "FROM Inventory AS I left join LotAttribute AS L  on I.lot=L.lot  and I.whseid=L.whseid WHERE  L.udf1='" + serial+ "' AND I.whseid='" + whseid + "' and (I.qty-I.allocated_qty-I.picked_qty)>0";
                    }
                    else
                    {
                        if (master_label.Length == 6) //扫描为库位
                        {
                            strSql = "SELECT I.customer_code,I.sku,L.lottable01,I.loc,I.lot,I.lpn,(I.qty-I.allocated_qty-I.picked_qty) as qty  ";
                            strSql += "FROM Inventory AS I left join LotAttribute AS L  on I.lot=L.lot  and I.whseid=L.whseid WHERE I.loc='" + loc + "' AND I.whseid='" + whseid + "' and (I.qty-I.allocated_qty-I.picked_qty)>0";
                        }
                        else  
                        {
                            if (master_label.StartsWith("L")) //扫描为托盘号
                            {
                                strSql = "SELECT I.customer_code,I.sku,L.lottable01,I.loc,I.lot,I.lpn,(I.qty-I.allocated_qty-I.picked_qty) as qty  ";
                                strSql += "FROM Inventory AS I left join LotAttribute AS L  on I.lot=L.lot  and I.whseid=L.whseid WHERE  I.lpn='" + slpn + "' AND I.whseid='" + whseid + "' and (I.qty-I.allocated_qty-I.picked_qty)>0";
                            }
                            else  //扫描为料号
                            {
                                strSql = "SELECT I.customer_code,I.sku,L.lottable01,I.loc,I.lot,I.lpn,(I.qty-I.allocated_qty-I.picked_qty) as qty  ";
                                strSql += "FROM Inventory AS I left join LotAttribute AS L  on I.lot=L.lot  and I.whseid=L.whseid WHERE  I.sku='" + sku_sm + "' AND I.whseid='" + whseid + "' and (I.qty-I.allocated_qty-I.picked_qty)>0";
                            }
                        }
                    }       
                }
             
                DataTable dt = App.GetDataTable(App.DBconWMS, strSql, "Sku");
                #endregion

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        #region
                        customer_code = dt.Rows[0]["customer_code"].ToString();
                        loc = dt.Rows[i]["loc"].ToString();
                        lot = dt.Rows[i]["lot"].ToString();
                        lpn = dt.Rows[i]["lpn"].ToString();
                        qty = Convert.ToDecimal(dt.Rows[i]["qty"].ToString()).ToString("#.##");
                        sku = dt.Rows[i]["sku"].ToString().Trim();
                        lottable01 = dt.Rows[i]["lottable01"].ToString();
                        #endregion

                        #region 构建XML报文
                        XmlNode sku_result = docreturn.CreateElement("sku_return");

                        XmlElement useridResult = docreturn.CreateElement("userid");
                        useridResult.InnerText = userid;
                        sku_result.AppendChild(useridResult);

                        XmlElement whseidResult = docreturn.CreateElement("whseid");
                        whseidResult.InnerText = whseid;
                        sku_result.AppendChild(whseidResult);

                        XmlElement master_labelResult = docreturn.CreateElement("master_label");
                        master_labelResult.InnerText = master_label;
                        sku_result.AppendChild(master_labelResult);

                        XmlElement customer_codeResult = docreturn.CreateElement("customer_code");
                        customer_codeResult.InnerText = customer_code;
                        sku_result.AppendChild(customer_codeResult);

                        XmlElement skuResult = docreturn.CreateElement("sku");
                        skuResult.InnerText = sku;
                        sku_result.AppendChild(skuResult);

                        XmlElement batchResult = docreturn.CreateElement("batch");
                        batchResult.InnerText = lottable01;
                        sku_result.AppendChild(batchResult);

                        XmlElement locResult = docreturn.CreateElement("loc");
                        locResult.InnerText = loc;
                        sku_result.AppendChild(locResult);

                        XmlElement lotResult = docreturn.CreateElement("lot");
                        lotResult.InnerText = lot;
                        sku_result.AppendChild(lotResult);

                        XmlElement lpnResult = docreturn.CreateElement("lpn");
                        lpnResult.InnerText = lpn;
                        sku_result.AppendChild(lpnResult);

                        XmlElement qtyResult = docreturn.CreateElement("qty");
                        qtyResult.InnerText = qty;
                        sku_result.AppendChild(qtyResult);

                        XmlElement return_statusResult = docreturn.CreateElement("return_status");
                        return_statusResult.InnerText = return_status;
                        sku_result.AppendChild(return_statusResult);

                        rootResult.AppendChild(sku_result);
                        #endregion
                    }
                }
                else
                {
                    #region 构建失败XML报文
                    return_status = "读取库存信息失败。 可能原因：1、不存在此料号库存信息  2、料号所在厂区与PDA登录账号的隶属厂区"+whseid+"不符";
                    XmlNode sku_result = docreturn.CreateElement("sku_return");

                    XmlElement useridResult = docreturn.CreateElement("userid");
                    useridResult.InnerText = userid;
                    sku_result.AppendChild(useridResult);

                    XmlElement whseidResult = docreturn.CreateElement("whseid");
                    whseidResult.InnerText = whseid;
                    sku_result.AppendChild(whseidResult);

                    XmlElement master_labelResult = docreturn.CreateElement("master_label");
                    master_labelResult.InnerText = master_label;
                    sku_result.AppendChild(master_labelResult);

                    XmlElement customer_codeResult = docreturn.CreateElement("customer_code");
                    customer_codeResult.InnerText = customer_code;
                    sku_result.AppendChild(customer_codeResult);

                    XmlElement skuResult = docreturn.CreateElement("sku");
                    skuResult.InnerText = sku;
                    sku_result.AppendChild(skuResult);

                    XmlElement batchResult = docreturn.CreateElement("batch");
                    batchResult.InnerText = lottable01;
                    sku_result.AppendChild(batchResult);

                    XmlElement locResult = docreturn.CreateElement("loc");
                    locResult.InnerText = loc;
                    sku_result.AppendChild(locResult);

                    XmlElement lotResult = docreturn.CreateElement("lot");
                    lotResult.InnerText = lot;
                    sku_result.AppendChild(lotResult);

                    XmlElement lpnResult = docreturn.CreateElement("lpn");
                    lpnResult.InnerText = lpn;
                    sku_result.AppendChild(lpnResult);

                    XmlElement qtyResult = docreturn.CreateElement("qty");
                    qtyResult.InnerText = qty;
                    sku_result.AppendChild(qtyResult);

                    XmlElement return_statusResult = docreturn.CreateElement("return_status");
                    return_statusResult.InnerText = return_status;
                    sku_result.AppendChild(return_statusResult);

                    rootResult.AppendChild(sku_result);
                    #endregion
                }

            }
            catch (Exception ex)
            {
                App.Log("PDAXmlMLInLotProcess.cs-1", ex.Message.ToString());
                //throw new Exception("异常源：PDAXmlMLInLotProcess:GetPDAMLInLotData;" + "  " + ex.Message);
            }
            docreturn.AppendChild(rootResult);
            return docreturn.InnerXml;
        }

    }
}
